export default {
  data () {
    return {
      // 商品分类数据
      categoriesList: [],
      total: 1,
      // 添加分类数据
      dialogAddCatVisible: false,
      addCatForm: {
        cat_name: '',
        cat_pid_arr: []
      },
      // 二级分类列表数据
      secondCalOptions: [],
      options: [{
        value: 'zhinan',
        label: '指南',
        children: [{
          value: 'shejiyuanze',
          label: '设计原则',
          children: [{
            value: 'yizhi',
            label: '一致'
          }, {
            value: 'fankui',
            label: '反馈'
          }, {
            value: 'xiaolv',
            label: '效率'
          }, {
            value: 'kekong',
            label: '可控'
          }]
        }, {
          value: 'daohang',
          label: '导航',
          children: [{
            value: 'cexiangdaohang',
            label: '侧向导航'
          }, {
            value: 'dingbudaohang',
            label: '顶部导航'
          }]
        }]
      }]
    }
  },
  created () {
    // 获取商品分类
    this.getCategoriesList()
    // 获取商品分类二级列表
    this.geyCatSecondList()
  },
  methods: {
    // 获取商品分类
    async getCategoriesList (pagenum = 1) {
      const res = await this.$axios.get('categories', {
        params: {
          type: 3,
          pagenum,
          pagesize: 4
        }
      })
      // console.log(res)
      this.categoriesList = res.data.data.result
      this.total = res.data.data.total
    },
    // 获取商品分类二级列表
    async geyCatSecondList () {
      const res = await this.$axios.get('categories', {
        params: {
          type: 2
        }
      })
      // console.log(res)
      this.secondCalOptions = res.data.data
    },
    // 打开添加分类对话框
    openAddCalDialog () {
      this.dialogAddCatVisible = true
    },
    // 添加分类
    async addCategoryBtn () {
      /* eslint-disable */
      let {
        cat_name,
        cat_pid_arr
      } = this.addCatForm
      let catPid = cat_pid_arr[cat_pid_arr.length - 1]
      let catPevel = cat_pid_arr.length
      await this.$axios.post('categories', {
        cat_name,
        cat_pid: catPid,
        cat_level: catPevel
      })
      this.dialogAddCatVisible = false
      this.getCategoriesList()
      this.$message({
        type: 'success',
        message: '添加分类成功'
      })
    },
    closeAddCal() {
      this.$refs.addCatForm.resetFields()
    },
    // 当前页改变
    pageChange(pagenum) {
      // console.log(pagenum)
      this.getCategoriesList(pagenum)
    }
  }
}
